package com.illtamer.infinite.sillage.config;

import com.illtamer.infinite.sillage.annotation.handler.ConvertHandler;
import com.illtamer.infinite.sillage.annotation.handler.SecurityUserHandler;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@Configuration
public class WebMVCConfiguration implements WebMvcConfigurer {

    @Resource(name = "securityUserHandler")
    private SecurityUserHandler securityUserHandler;

    @Resource(name = "convertHandler")
    private ConvertHandler convertHandler;

    /**
     * 自定义资源路径
     * @apiNote 兼容前端 plugin 访问以 /static/ 开头的资源
     * @see WebMvcProperties#getStaticPathPattern()
     * @see WebProperties.Resources#getStaticLocations()
     * */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(
                "/static/**", "/**"
        ).addResourceLocations("classpath:/static/");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.addAll(Arrays.asList(securityUserHandler, convertHandler));
    }
}
